package com.pybox.aop;

import com.alibaba.fastjson.JSON;
import com.pybox.entity.TCanbinetEventLogs;
import com.pybox.entity.TServerLogs;
import com.pybox.service.TCanbinetEventLogsService;
import com.pybox.service.TServerLogService;
import com.pybox.util.IpUtil;
import com.pybox.util.UserUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.sql.Timestamp;

/**
 * @author lei
 * @title: SysLogAspect
 * @projectName pybox
 * @description: 系统日志：切面处理类
 * @date 2019/5/30 15:20
 */


@Aspect
@Component
public class SysLogAspect {

    //注入操作日志管理类的service
    @Autowired
    private TServerLogService tServerLogService;
    //定义切点 @Pointcut
    //在注解的位置切入代码
    @Pointcut("@annotation(com.pybox.aop.Mylog)")
    public void logPoinCut() {
    }

    //切面 配置通知
    @AfterReturning("logPoinCut()")
    public void saveSysLog(JoinPoint joinPoint) {
        //System.out.println("切面。。。。。");
        //保存日志
        TServerLogs sysLog = new TServerLogs();

        //从切面织入点处通过反射机制获取织入点处的方法
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        //获取切入点所在的方法
        Method method = signature.getMethod();
        //获取操作
        Mylog myLog = method.getAnnotation(Mylog.class);
        if (myLog != null) {
            String value = myLog.value();
            sysLog.setOperate(value);//保存获取的操作

        }

        //获取请求的类名
        String className = joinPoint.getTarget().getClass().getName();
        sysLog.setOperateclass(className);
        //获取请求的方法名
        sysLog.setOperatemethod(method.getName());
        //获取当前操作的客户id
        Integer customerId = UserUtils.getCustomerId();
        sysLog.setCustomer_id(customerId);
        //请求的参数
        Object[] args = joinPoint.getArgs();
        //将参数所在的数组转换成json
        String params = JSON.toJSONString(args);
        //sysLog.setOperateparams(params);
        //服务端操作时间
        Timestamp createTime = new Timestamp(System.currentTimeMillis());
        sysLog.setCtime(createTime);
        //获取用户名
        sysLog.setUser_id(UserUtils.getUserId());
        sysLog.setRealname(UserUtils.getCurrentUser().getRealname());
        //获取用户ip地址
        //获取RequestAttributes
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        //从获取RequestAttributes中获取HttpServletRequest的信息
        HttpServletRequest request = (HttpServletRequest) requestAttributes.resolveReference(RequestAttributes.REFERENCE_REQUEST);
        //获取操作的ip
        sysLog.setIpds(IpUtil.getIpAddr(request));
        //调用service保存SysLog实体类到数据库
        tServerLogService.save(sysLog);
    }

}
